package com.jiawei.note.net;

import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;


public class RetrofitManager {
    private RetrofitManager(){}
    private static RetrofitManager retrofitManager;
    public static RetrofitManager getInstance(){
        if(retrofitManager == null){
            synchronized (RetrofitManager.class){
                if(retrofitManager == null){
                    retrofitManager = new RetrofitManager();
                }
            }
        }
        return retrofitManager;
    }

    private Retrofit mRetrofit;

    public Retrofit getRetrofit() {
        if(mRetrofit == null){
            createRetrofit();
        }
        return mRetrofit;
    }

    private void createRetrofit() {
        OkHttpClient client = new OkHttpClient.Builder()
                .writeTimeout(60, TimeUnit.SECONDS)
                .readTimeout(60, TimeUnit.SECONDS)
                .addInterceptor(new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY))
                .build();


        mRetrofit = new Retrofit.Builder()
                .baseUrl("http://10.161.9.80:9999")
                .client(client)
                .addConverterFactory(GsonConverterFactory.create())//gson解析器，不需要手动解析
               // .addCallAdapterFactory(RxJava2CallAdapterFactory.create())//将retrofit默认返回的Call转成Rxjava的Observerable
                .addCallAdapterFactory(new LiveDataCallAdapterFactory())//将retrofit默认返回的Call转成liveData
                .build();

    }
}
